/**
*  \file cromo.c
*
*  \brief  Continuous ROM Observer
*
*  \author Michael J. Hohmann  <Michael.Hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#define CROMO_LINKAGE
#include "cromo.h"
//#include "assertor.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/

/*===============================================[ public variables  ]================================================*/

/*===============================================[ private functions ]================================================*/

/*====================================================================================================================*/
static void cromo_reset(struct cromo *me);

/*====================================================================================================================*/

RC cromo_initialize( struct cromo *me, cromo_cks_t checksum, cromo_data_t *start,U32 len, cromo_callback_t function)
{
	//ASSERTOR_PRE( NULL != me , "Object pointer can't be NULL." );
	//ASSERTOR_PRE( NULL != function, "Callback pointer can't be NULL.");

	me->pbegin=start; 		  /* pointer to the start of the memory chunk */
	me->len=len; 			  /* number of words to be calculated */
	me->checksum=checksum; 	  /* The expected checksumm of the whole memory block */
	//me->cks_init=init_value;  /* ini value of the checksumm algorithm */
	me->function=function;    /* checksumm calculation function */
	me->succeed=0;  			  /* counter of positive runs */
	me->failed =0;            /* counter of negative runs */
	cromo_reset(me); 			  /* reset to start of memory block */

	return RC_SUCCESS;
}

S16 cromo_process( struct cromo *me, U32 len)
{
	enum cromo_func_status status;

	//ASSERTOR_PRE( NULL != me , "Object pointer can't be NULL." );

	/* cut len if we are at the end of the memory block */
	if( len > me->todo )
	{
		len = me->todo;
	}

    /* calculate the checksum */
//	ASSERTOR_CHK( NULL != me->function , "Object pointer can't be NULL." );

	if ( me->cursor==me->pbegin )
	{
		status = cromo_func_status_first;
	}
	else
	{
		if( me->todo == len )
		{
			status = cromo_func_status_last;

		}
		else
		{
			status = cromo_func_status_pending;

		}
	}
	me->subtotal = me->function(me->cursor, len, status );

	/* adjust pointer */
	me->cursor+=len;
	me->todo-=len;

	if (0==me->todo) /* calculation completed */
	{
		if (me->subtotal == me->checksum) /* is the checksum correct */
		{
		   if( S16_MAX > me->succeed )
		   {
		      me->succeed++;
		   }
		}
		else
		{
         if( S16_MIN < me->failed )
         {
            me->failed--;
         }

		}
		cromo_reset(me); /* reset pointer and counter to the start of memory block */
	}

	return cromo_status( me);
}


S16 cromo_status( struct cromo *me)
{
	//ASSERTOR_PRE( NULL != me , "Object pointer can't be NULL." );

	if( 0 > me->failed )
	{
		return me->failed;
	}
	else
	{
		return me->succeed;
	}
}


/** Reset internal values
*
* The function rest() will set pointer to start of memory block.
* Reset the counter of the pending words to check and initialize the checksum.
*/
static void cromo_reset(struct cromo *me)
{
	//ASSERTOR_PRE( NULL != me , "Object pointer can't be NULL." );

	/* reset internals */
	me->cursor  = me->pbegin; /* start of memory block */
	me->todo    = me->len;    /* number of words to be processed */
	//me->subtotal= me->cks_init; /* initial value of checksum algorithm */
}

/***********************************************************************************************************************
*                             Copyright 2009 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
